package im.oen.gui.tools.window.model;

import im.oen.boot.common.utils.Checker;
import im.oen.gui.tools.cache.ConnectCache;
import im.oen.gui.tools.component.JModel;
import im.oen.gui.tools.component.JPassword;
import im.oen.gui.tools.data.connect.MysqlConnectData;
import im.oen.gui.tools.support.mysql.Connection;
import im.oen.gui.tools.util.MsgUtil;
import im.oen.gui.tools.util.SwingUtil;
import im.oen.gui.tools.window.MainFrame;

import javax.swing.*;

/**
 * @author Kevin.Xu
 */
public class MysqlConnect extends JModel {

    private MainFrame frame;

    public MysqlConnect(final MainFrame frame) {
        super(frame, MsgUtil.get("title.connect.mysql"), 300, 260);
        this.frame = frame;
        this.init();
    }

    private void init() {
        this.setLayout(null);

        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.ip", " :"), 20, 10, 50, 20));
        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.port", " :"), 30, 40, 40, 20));
        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.database", " :"), 4, 70, 66, 20));
        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.user", " :"), 30, 100, 40, 20));
        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.pass", " :"), 30, 130, 40, 20));
        this.addComponent(SwingUtil.createLabel(MsgUtil.suffix("connect.mysql.nick", " :"), 30, 160, 40, 20));

        JTextField ipTxt = SwingUtil.createTextField("127.0.0.1", 70, 10, 200, 20);
        JTextField portTxt = SwingUtil.createTextField("3306", 70, 40, 200, 20);
        JTextField dbTxt = SwingUtil.createTextField("test", 70, 70, 200, 20);
        JTextField userTxt = SwingUtil.createTextField("root", 70, 100, 200, 20);
        JPassword passTxt = SwingUtil.createTextField('*', 70, 130, 200, 20);
        JTextField nickTxt = SwingUtil.createTextField("localhost", 70, 160, 200, 20);

        final MysqlConnectData data = new MysqlConnectData();
        JButton btnTest = SwingUtil.createButton(MsgUtil.get("connect.mysql.btn.test"), 30, 190, 100, 30);
        btnTest.addActionListener((evt) -> {
            data.init(nickTxt.getText(), ipTxt.getText(), portTxt.getText(), dbTxt.getText(), userTxt.getText(), passTxt.getPass());
            Connection conn = MysqlConnect.this.testMysqlConnect(data);
            MysqlConnect.this.showTestResult(Checker.isNotEmpty(conn), true);
        });
        JButton btnOK = SwingUtil.createButton(MsgUtil.get("global.btn.ok"), 170, 190, 100, 30);
        btnOK.addActionListener((evt) -> {
            data.init(nickTxt.getText(), ipTxt.getText(), portTxt.getText(), dbTxt.getText(), userTxt.getText(), passTxt.getPass());
            ConnectCache.add(data);
            MysqlConnect.this.frame.syncConnect();
            MysqlConnect.this.removeSelf();
        });
        this.addComponent(ipTxt, portTxt, dbTxt, userTxt, passTxt, nickTxt, btnTest, btnOK);
    }

    private void showTestResult(boolean success, boolean showSuccess) {
        if (success) {
            if (showSuccess) {
                JOptionPane.showMessageDialog(MysqlConnect.this, MsgUtil.get("connect.mysql.tip.test.succ"), MsgUtil.get("connect.mysql.tip.title"), JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(MysqlConnect.this, MsgUtil.get("connect.mysql.tip.test.fail"), MsgUtil.get("connect.mysql.tip.title"), JOptionPane.ERROR_MESSAGE);
        }
    }

    private Connection testMysqlConnect(MysqlConnectData data) {
        if (Checker.isEmpty(data)) {
            return null;
        }
        return new Connection(data);
    }
}
